package com.aptech.oas.action;

import java.util.Map;

import org.hibernate.Session;

import com.aptech.oas.dao.UserDao;
import com.aptech.oas.dao.UserDaoImpl;
import com.aptech.oas.model.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private String username;
	private String password;
	private String message="";
	private UserDao userDao=new UserDaoImpl();

	
	
	public String login(){
		User user=userDao.selectUserByWorkNo(username);
		
		ActionContext context=ActionContext.getContext();
		Map<String, Object> session=context.getSession();
		if(session.containsKey("loginUser")||session.containsKey("userAuthority")){
			session.remove("loginUser");
			session.remove("userAuthority");
		}
		if(username!=null ){
			if(password!=null){
				if(user!=null){
					if(user.getPw().equals(password)){
						session.put("loginUser", user.getWorkNo());
						session.put("userAuthority",user.getAuthority());
						return SUCCESS;
					}else{
						message="密码错误";
					}
				}else{
					message="工号错误";
				}
			}else{
				message="密码错误";
			}
		}else{
			message="工号错误";
		}
		return LOGIN;
	}
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
}
